<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>

	<script>
		var socket;
		function openSocket() {
			if (typeof (WebSocket) == "undefined") {
				console.log("您的浏览器不支持WebSocket");
			} else {
				console.log("您的浏览器支持WebSocket");
				//实现化WebSocket对象，指定要连接的服务器地址与端口  建立连接
				var userId = document.getElementById('userId').value;
				alert(userId);
				//  var socketUrl="ws://10.6.1.220:8888/wos/ws/"+userId;
				// var socketUrl = "ws://127.0.0.1:8888/wos/ws/" + userId;
			//	 var socketUrl = "ws://10.6.129.241:8888/wos/ws/" + userId;
				 var socketUrl = "ws://59.211.213.13/wos/ws/" + userId;

				console.log(socketUrl);
				if (socket != null) {
					socket.close();
					socket = null;
				}
				socket = new WebSocket(socketUrl);
				//打开事件
				socket.onopen = function() {
					console.log("websocket已打开");
					//socket.send("这是来自客户端的消息" + location.href + new Date());
				};
				//获得消息事件
				socket.onmessage = function(msg) {
					var serverMsg = "收到服务端信息：" + msg.data;
					console.log(serverMsg);
					//发现消息进入    开始处理前端触发逻辑
				};
				//关闭事件
				socket.onclose = function() {
					console.log("websocket已关闭");
				};
				//发生了错误事件
				socket.onerror = function() {
					console.log("websocket发生了错误");
				}
			}
		}
		function sendMessage() {
			if (typeof (WebSocket) == "undefined") {
				console.log("您的浏览器不支持WebSocket");
			} else {
				// console.log("您的浏览器支持WebSocket");
				var toUserId = document.getElementById('toUserId').value;
				var contentText = document.getElementById('contentText').value;
				var msg = '{"toUserId":"' + toUserId + '","contentText":"'
						+ contentText + '"}';
				console.log(msg);
				socket.send(msg);
			}
		}
		
		// init
		
var Ajax={
  get: function(url, fn) {
    // XMLHttpRequest对象用于在后台与服务器交换数据   
    var xhr = new XMLHttpRequest();            
    xhr.open('GET', url, true);
    xhr.onreadystatechange = function() {
      // readyState == 4说明请求已完成
      if (xhr.readyState == 4 && xhr.status == 200 || xhr.status == 304) { 
        // 从服务器获得数据 
        console.log(xhr.responseText);
        init(xhr.responseText);
      //  fn.call(this, xhr.responseText);  
      }
    };
    xhr.send();
  } 
}
		
		function init(text){
			var jsonData = JSON.parse(text) ;
	console.log(jsonData);

	var allL = 0 ;
	for(var i=0;i<jsonData.data.length;i++){
		var ob = jsonData.data[i] ;
		allL += ob.count ;
		if(ob.status == 1){
			// 
			document.getElementById("daishenhe").value = ob.count ;
		}
	}
	document.getElementById("all").value = allL ;

			console.info(jsonData.data);
		}
	Ajax.get("./taskCommon/count/task");
	 
		// wos/send 
		// 服务主动推送消息
	</script>
</body>


<p>【全部】：<div><input id="all"  type="text" value="10"></div> 
<p>【待审核】：<div><input id="daishenhe"  type="text" value="10"></div> 
  



<p>【userId】：<div><input id="userId" name="userId" type="hidden" value="10"></div>
<p>【toUserId】：<div><input id="toUserId" name="toUserId" type="hidden" value="20"></div>
<p>【toUserId】：<div><input id="contentText" name="contentText" type="hidden" value="hello websocket"></div>
<p>操作:<div>  <button onclick="openSocket()">开启socket </button>  </div>
<p>【操作】： <button onclick="sendMessage()">发送消息</button>   </div>

</html>